/**
 * @product: PCDC-CMS
 * @copyright: FSAI-南征北战信息科技
 * @filename: com.fsai.cms.service.impl.CmsPubServiceImpl.java
 *
 * @link: Http://www.fastsai.com
 * @jdkv: Jdk 1.8
 */
package com.fsai.cms.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fsai.cms.base.WebCons;
import com.fsai.cms.domain.BaseLeader;
import com.fsai.cms.domain.BaseOrg;
import com.fsai.cms.domain.BaseSiteCfg;
import com.fsai.cms.domain.CmsCarouselPic;
import com.fsai.cms.domain.CmsEduVideo;
import com.fsai.cms.domain.CmsFriendlyLinks;
import com.fsai.cms.domain.CmsIndexBanner;
import com.fsai.cms.domain.CmsSpread;
import com.fsai.cms.domain.PageAboutUs;
import com.fsai.cms.domain.PageCenter;
import com.fsai.cms.domain.PageCommon;
import com.fsai.cms.domain.PageRepairUpgrade;
import com.fsai.cms.mapper.BaseLeaderMapper;
import com.fsai.cms.mapper.BaseOrgMapper;
import com.fsai.cms.mapper.BaseSiteCfgMapper;
import com.fsai.cms.mapper.CmsCarouselPicMapper;
import com.fsai.cms.mapper.CmsEduVideoMapper;
import com.fsai.cms.mapper.CmsFriendlyLinksMapper;
import com.fsai.cms.mapper.CmsIndexBannerMapper;
import com.fsai.cms.mapper.CmsSpreadMapper;
import com.fsai.cms.mapper.ISqlMapper;
import com.fsai.cms.mapper.PageCenterMapper;
import com.fsai.cms.service.ICmsPubService;
import com.fsai.cms.util.BeanMapTool;
import com.fsai.cms.util.BeanUtilsTool;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 *
 * @project: pcdc-cms-web
 *
 * @author: JaffeHe
 * @email: JaffeHe@foxmail.com
 * @date: 2021-03-13 18:32
 *
 * @ChangeLog<br/>
 * 				Developer Date Comment <br/>
 */
@Service
public class CmsPubServiceImpl implements ICmsPubService {

	@Autowired
	private BaseSiteCfgMapper baseSiteCfgMapper;
	@Autowired
	private BaseLeaderMapper leaderMapper;
	@Autowired
	private CmsCarouselPicMapper carouselPicMapper;
	@Autowired
	private CmsFriendlyLinksMapper friendlyLinksMapper;
	@Autowired
	private BaseOrgMapper orgMapper;
	@Autowired
	private CmsSpreadMapper spreadMapper;
	@Autowired
	private PageCenterMapper pageCenterMapper;
	@Autowired
	private CmsIndexBannerMapper indexBannerMapper;
	@Autowired
	private CmsEduVideoMapper eduVideoMapper;

	@Autowired
	private ISqlMapper sqlMapper;

	@Override
	public BaseSiteCfg selSiteInfo(Integer mainId) {
		return baseSiteCfgMapper.selectBaseSiteCfgById(mainId);
	}

	@Override
	public String getSycConfVal(String key) {
		String sql = "SELECT config_value from sys_config where config_key='" + key + "' ";
		return sqlMapper.selectOneProp(sql);
	}

	@Override
	public int findOrgTopCateId(String orgCode) {
		String sql = "SELECT min(cate_id) as re from cms_cate where cate_code='" + orgCode + "' ";
		return Integer.parseInt(sqlMapper.selectOneProp(sql));
	}

	@Override
	public String findOrgIdByCode(String orgCode) {
		String sql = "SELECT org_id from base_org where org_code='" + orgCode + "' ";
		return sqlMapper.selectOneProp(sql);
	}

	@Override
	public List<BaseLeader> findOrgLeaders(String orgCode) {
		String orgId = this.findOrgIdByCode(orgCode);
		BaseLeader query = new BaseLeader();
		query.setOrgId(Long.parseLong(orgId));
		query.setStatus(WebCons.STATUS_Y);
		List<BaseLeader> leaderList = leaderMapper.selectBaseLeaderList(query);

		//		if (siteCfg.isUrltrans()) {
		//			for (BaseLeader baseLeader : leaderList) {
		//				baseLeader.setLeaderPhoto(UrlPathTool.getUri(baseLeader.getLeaderPhoto()));
		//			}
		//		}

		return leaderList;
	}

	@Override
	public List<CmsCarouselPic> findOrgCarouselPics(String orgCode) {
		String orgId = this.findOrgIdByCode(orgCode);

		CmsCarouselPic query = new CmsCarouselPic();
		query.setOrgId(Long.parseLong(orgId));
		query.setStatus(WebCons.STATUS_Y);

		return carouselPicMapper.selectCmsCarouselPicList(query);
	}

	@Override
	public List<CmsFriendlyLinks> selectFriendLinks() {
		CmsFriendlyLinks query = new CmsFriendlyLinks();
		query.setStatus(WebCons.STATUS_Y);

		return friendlyLinksMapper.selectCmsFriendlyLinksList(query);
	}

	@Override
	public List<BaseOrg> findSubOrg() {
		BaseOrg query = new BaseOrg();
		query.setOrgType(WebCons.SUB_ORG_TYPE);
		query.setStatus(WebCons.STATUS_Y);

		List<BaseOrg> orgList = orgMapper.selectBaseOrgList(query);
		return orgList;
	}

	@Override
	public List<CmsSpread> selectSpreads() {
		CmsSpread query = new CmsSpread();
		query.setStatus(WebCons.STATUS_Y);

		return spreadMapper.selectCmsSpreadList(query);
	}

	@Override
	public PageCenter findPageCenter() {
		String sql = "SELECT max(pc_id) as re from page_center where status='" + WebCons.STATUS_Y + "'  ";
		int maxId = Integer.parseInt(sqlMapper.selectOneProp(sql));

		return pageCenterMapper.selectPageCenterById(maxId);
	}

	@Override
	public CmsIndexBanner findIndexBanner() {
		String sql = "SELECT max(cip_id) as re from cms_index_banner where status='" + WebCons.STATUS_Y + "'  ";
		long maxId = Long.parseLong(sqlMapper.selectOneProp(sql));

		return indexBannerMapper.selectCmsIndexBannerById(maxId);
	}

	@Override
	public List<CmsEduVideo> selectTopEduVideo(Integer count) {
		return eduVideoMapper.selectTopEduVideo(count);
	}

	@Override
	public BaseOrg findOrgInfoByCode(String orgCode) {
		BaseOrg query = new BaseOrg();
		query.setOrgCode(orgCode);

		return orgMapper.selectBaseOrgByParam(query);
	}

	@Override
	public PageAboutUs findAboutUsByOrgCode(String orgCode) {
		String sql = "SELECT * from page_about_us where status='0' and org_id=(select org_id from base_org where org_code='" + orgCode
				+ "') order by update_time desc limit 1";
		Map<String, Object> oneObject = sqlMapper.selectOneObject(sql);

		try {
			return BeanMapTool.sqlMapToBean(oneObject, PageAboutUs.class);
		} catch (Exception e) {
			return new PageAboutUs();
		}
	}

	@Override
	public PageRepairUpgrade findRepairUpgrade() {
		String sql = "select * from page_repair_upgrade where status='0' order by update_time desc limit 0,1 ";
		Map<String, Object> oneObject = sqlMapper.selectOneObject(sql);

		try {
			return BeanUtilsTool.sqlMapToBean(oneObject, PageRepairUpgrade.class);
		} catch (Exception e) {
			return new PageRepairUpgrade();
		}
	}

	@Override
	public int saveContVisitLog(String contId, String ip) {
		//0插入日志表
		String sql = "insert into cms_cont_hislog (visit_ip,cont_id,visit_time,remark) values('" + ip + "','" + contId
				+ "',NOW(),'用户来访')";
		Integer insert = sqlMapper.insert(sql);
		if (insert > 0) {
			//1更新访问次数
			sql = "update cms_cont set hit_count=IFNULL(hit_count,0)+1 where cont_id=" + contId;
			Integer update = sqlMapper.update(sql);
			return update;
		}

		return 0;
	}

	@Override
	public CmsEduVideo findVideoById(String vId) {
		return eduVideoMapper.selectCmsEduVideoById(Long.parseLong(vId));
	}

	@Override
	public PageInfo<CmsEduVideo> queryVideoPageData(CmsEduVideo query, int pageSize, int pageNum) {
		if (pageNum > 0) {
			PageHelper.startPage(pageNum, pageSize);
		}
		PageHelper.orderBy("sort_index asc");

		return new PageInfo<CmsEduVideo>(eduVideoMapper.selectCmsEduVideoList(query));
	}

	@Override
	public PageCommon queryPageInfoByCode(String pgCode) {
		String sql = "select * from page_common where status='0' and pg_code='" + pgCode
				+ "' order by update_time desc limit 0,1 ";
		Map<String, Object> oneObject = sqlMapper.selectOneObject(sql);

		try {
			return BeanUtilsTool.sqlMapToBean(oneObject, PageCommon.class);
		} catch (Exception e) {
			return new PageCommon();
		}
	}

}
